package cn.jhc.sevlet;//登录Servlet

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import cn.jhc.dao.UserVerify;
import cn.jhc.dao.impl.StudentDaoImpl;
import cn.jhc.dao.impl.TeacherDaoImpl;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
	private static Logger logger = Logger.getLogger(LoginServlet.class.getName());
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.sendRedirect("login.jsp");
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info("post");
		UserVerify userVerify = null;
		String role = request.getParameter("role");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		logger.info(role + "," + username + "," + password);
		if (role != null && !role.equals("")) {
			if (role.equals("1")) {
				userVerify = new StudentDaoImpl();
			}else if(role.equals("2")){
				userVerify = new TeacherDaoImpl();
			}
		}
		if (userVerify != null && userVerify.verify(username, password) ) {
			
			HttpSession session = request.getSession();
			session.setAttribute("role", role);
			session.setAttribute("username", username);
			 
			response.sendRedirect(request.getContextPath() + "/user/index.jsp");
			return;
		}
		response.sendRedirect("login.jsp");
	}

}
